/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.data;

import java.util.Arrays;
import java.util.Date;

import com.rapplogic.xbee.api.zigbee.ZNetRxResponse;
import com.rapplogic.xbee.util.ByteUtils;

/**
 * Class for storing data from Xbee packets containing Dylos Data
 * 
 * @author Kyle O'Connor
 * 
 */
public class DylosData {

	ZNetRxResponse xbeePacket;
	Date date;
	int[] particleCounts;
	boolean isDeviceIdentification;
	String nodeID;

	public DylosData(ZNetRxResponse packet, String id) {
		particleCounts = new int[2];
		this.xbeePacket = packet;
		this.date = new Date();
		this.nodeID = id;
		
		parseData();
	}

	/**
	 * @return a trimmed String representation of the data
	 */
	private String getDataAsString() {
		return ByteUtils.toString(xbeePacket.getData()).trim();
	}

	private void parseData() {
		String strData = this.getDataAsString();
		if (strData.contains(",")) {
			// particle count data
			String[] strPC = strData.split(",");
			if (strPC.length == 2) {
				particleCounts[0] = Integer.parseInt(strPC[0]);
				particleCounts[1] = Integer.parseInt(strPC[1]);
				isDeviceIdentification = false;
			} else {
				// bad parse
				isDeviceIdentification = true;
			}
		} else {
			// device identification
			isDeviceIdentification = true;
		}
	}

	/**
	 * @return the isDeviceIdentification
	 */
	public boolean isDeviceIdentification() {
		return isDeviceIdentification;
	}

	/**
	 * @return the xbeePacket
	 */
	public ZNetRxResponse getXbeePacket() {
		return xbeePacket;
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * @return the particleCounts
	 */
	public int[] getParticleCounts() {
		return particleCounts;
	}

	public String getNodeID() {
		return nodeID;
	}

	public void setNodeID(String nodeID) {
		this.nodeID = nodeID;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + (isDeviceIdentification ? 1231 : 1237);
		result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode());
		result = prime * result + Arrays.hashCode(particleCounts);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DylosData other = (DylosData) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (isDeviceIdentification != other.isDeviceIdentification)
			return false;
		if (nodeID == null) {
			if (other.nodeID != null)
				return false;
		} else if (!nodeID.equals(other.nodeID))
			return false;
		if (!Arrays.equals(particleCounts, other.particleCounts))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "DylosData [date=" + date + ", isDeviceIdentification=" + isDeviceIdentification + ", nodeID=" + nodeID
				+ ", particleCounts=" + Arrays.toString(particleCounts) + "]";
	}
}
